package com.cat.greedyAlgorithm;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/group-the-people-given-the-group-size-they-belong-to/?envType=problem-list-v2&envId=GT3MFwwC
 * @create 2025/10/3 21:49
 * @since JDK17
 */

public class Solution49 {
    public List<List<Integer>> groupThePeople(int[] groupSizes) {
        int n = groupSizes.length;
        List<Integer>[] list = new List[n + 1];
        Arrays.setAll(list, i -> new ArrayList<>());
        List<List<Integer>> ans = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list[groupSizes[i]].add(i);
            if (list[groupSizes[i]].size() == groupSizes[i]) {  // 人满了
                ans.add(new ArrayList<>(list[groupSizes[i]]));
                list[groupSizes[i]].clear();
            }
        }
        return ans;
    }
}
